function check(obj)
{
    obj.checked = true; 
}

function uncheck(obj)
{
    obj.checked = false;
}

function enable(obj)
{
    obj.disabled = false;
}

function disable(obj)
{
    obj.disabled = true;
}

function show(obj)
{
    obj.style.display="";
}

function hide(obj)
{
    obj.style.display="none";
}

function display(obj, isYes)
{
    if (isYes) show(obj);
    else hide(obj);
}

function isChecked(obj)
{
    return (obj.checked == true);
}

function isUnchecked(obj)
{
    return (obj.checked == false);
}

function isEnabled(obj)
{
    return (obj.disabled == false);
}

function setProperty(obj, property, value) {
var evalStr = "obj." + property + "=" + value + ";";
//alert(evalStr);
	eval(evalStr);
}

function setPropertyById(id, property, value) {
	var obj;

	if (obj = document.getElementById(id)) {
		setProperty(obj, property, value);
	}
}

function changeStyle(obj, property, value) {
	setProperty(obj, "style." + property, value);
}

function changeStyleById(id, property, value) {
	var obj;

	if (obj = document.getElementById(id)) {
		changeStyle(obj, property, value);
	}
}

function changeStyleByIds(idArray, property, value) {
	var i;

	for (i = 0; i < idArray.length; i++) {
		changeStyleById(idArray[i], property, value);
	}
}

function setPropertyByIds(idArray, property, value) {
	var i;

	for (i = 0; i < idArray.length; i++) {
		setPropertyById(idArray[i], property, value);
	}
}

function setPropertyByName(rootObj, elementNames, nameExp, property, value) {
	var i, j;
	var re = new RegExp(nameExp);
	var objElementArray;

	for (i = 0; i < elementNames.length; i++) {
		objElementArray = rootObj.getElementsByTagName(elementNames[i]);
		for (j = 0; j < objElementArray.length; j++) {
			if (re.test(objElementArray[j].name) && (!objElementArray[j].disabled || property == "disabled")) {
				setProperty(objElementArray[j], property, value);
			}
		}
	}
}

function hideElementsById(idArray, hide) {
	changeStyleByIds(idArray, "display", hide ? "'none'" : "''");
}

function disableElementsById(idArray, disabled) {
	setPropertyByIds(idArray, "disabled", disabled);
}

function disableElementsByName(rootObj, elementNames, nameExp, disabled) {
	setPropertyByName(rootObj, elementNames, nameExp, "disabled", disabled);
}

function checkInputByName(rootObj, nameExp, checked) {
	setPropertyByName(rootObj, ['input'], nameExp, "checked", checked);
}

function hideSection(controlObj, idArray) {
	var hidden = !(document.getElementById(idArray[0]).style.display == 'none');

	hideElementsById(idArray, hidden);

	if (hidden) {
		controlObj.src = "../images/twistie_collapsed.gif";
	} else {
		controlObj.src = "../images/twistie_expanded.gif";
	}
}

function implodeField(field, separator, onlySelected) {
	var i;
	var str = "";
	for (i = 0; i < field.length; i ++) {
		if (onlySelected) {
			if (field[i].selected) {
				if (str.length > 0) {
					str += separator;
				}
				str += field[i].value;
			}
		} else {
			if (str.length > 0) {
				str += separator;
			}
			str += field[i].value;
		}
	}
	return str;
}

function refreshFields(nameExp, elementNamesArray, refreshInterval) {
	var refreshFrameString = "refreshFrame";

	if (window.location.toString().indexOf(refreshFrameString, window.location.toString().length - refreshFrameString.length) > 0) {
		var i, j;
		var re = new RegExp(nameExp);
		var objElementArray;
		var refreshField;

		for (i = 0; i < elementNamesArray.length; i ++) {
			objElementArray = document.getElementsByTagName(elementNamesArray[i]);
			for (j = 0; j < objElementArray.length; j ++) {
				if (re.test(objElementArray[j].id)) {
					refreshField = parent.document.getElementById(objElementArray[j].id);
					if (refreshField) {
						refreshField.innerHTML = objElementArray[j].innerHTML;
						refreshField.setAttribute('style', objElementArray[j].getAttribute('style'));
						refreshField.style.cssText = objElementArray[j].style.cssText;
					}
				}
			}
		}

		if (refreshInterval) {
			window.setTimeout("window.location = window.location;", refreshInterval * 1000);
		}

	} else {
		var subFrame = document.createElement('iframe');

		subFrame.setAttribute('src', window.location.toString() + ((window.location.toString().split('?').length > 1) ? '&' : '?') + refreshFrameString);
		subFrame.setAttribute('name', 'refreshFrame');
		subFrame.setAttribute('height', '0');
		subFrame.setAttribute('width', '0');
		subFrame.setAttribute('frameborder', '0');
		document.body.appendChild(subFrame);
	}
}

function display_vdom_in_footer(title, vd_name)
{
    var vdom_sep = top.footer.document.getElementById('vdom_sep');
    var vdom_title = top.footer.document.getElementById('vdom_title');
    var vdom_val = top.footer.document.getElementById('vdom_val');

    if (vdom_sep && (vdom_sep.innerHTML == ""))
        vdom_sep.innerHTML= "<img src='./images/footer_separator.gif'>&nbsp;";
    if (vdom_title && (vdom_title.innerHTML== ""))
        vdom_title.innerHTML = "<span onclick=\"top.frames['mainnav'].refreshmenu('system', 'sysvirtualdomain'); top.frames['subnav'].location='/button?button=sysvirtualdomainlist&button_url=/system/virtualdomain/list&act=-1'; top.frames['main'].location='/system/virtualdomain/current';\" style=\"text-decoration: underline; cursor: pointer\">" + title + "</span>: ";
    if (vdom_val)
        vdom_val.innerHTML = vd_name;
}

function clear_vdom_in_footer()
{
    var vdom_sep = top.footer.document.getElementById('vdom_sep');
    var vdom_title = top.footer.document.getElementById('vdom_title');
    var vdom_val = top.footer.document.getElementById('vdom_val');

    if (vdom_sep)
        vdom_sep.innerHTML = "";
    if (vdom_title)
        vdom_title.innerHTML= "";
    if (vdom_val)
        vdom_val.innerHTML= "";
}

function clog_flg(flg_ip)
{
   n = window.open("", "flg");
   n.navigate("https://" + flg_ip);
   window.close();
}

function clog_link()
{
    if (window.opener)
        window.opener.top.main.location = '/log/setting/dlg';
    window.close();
}

function removeOptions(field) {
	var removedOptions = document.createElement("select");
	var i;
	var j = 0;

	for (i = 0; i < field.options.length; i ++) {
		if (field.options[i].selected) {
			removedOptions.options[j] = new Option();
			removedOptions.options[j].text = field.options[i].text;
			removedOptions.options[j].value = field.options[i].value;
			removedOptions.options[j].selected = true;

			field.options[i] = null;
			i --;
			j ++;
		}
	}

	return removedOptions;
}

function moveOptionField(fromField, toField) {
	var insertIndex = toField.options.length;
	var selectedOptions;
	var insertNumber = 0;
	var i;

	// find insertion point in destination field
	for (i = 0; i < toField.options.length; i ++) {
		if (toField.options[i].selected) {
				insertIndex = i + 1;
				toField.options[i].selected = false;
		}
	}

	// remove options from fromField
	selectedOptions = removeOptions(fromField);

	// add empty options by increasing length
	toField.options.length += selectedOptions.options.length;

	// shift old entries down
	for (i = (toField.options.length - 1); i >= (insertIndex + selectedOptions.options.length); i --) {
		toField.options[i].text = toField.options[i - selectedOptions.options.length].text;
		toField.options[i].value = toField.options[i - selectedOptions.options.length].value;
	}

	// insert new entries
	for (i = 0; i < selectedOptions.options.length; i ++, insertIndex ++) {
		toField.options[insertIndex].text = selectedOptions.options[i].text;
		toField.options[insertIndex].value = selectedOptions.options[i].value;
		toField.options[insertIndex].selected = true;
	}
}

function moveOptionUp(selectField) {
	var i;
	var oOption = new Option();
	var length = selectField.options.length;
	
	if (length == 0 || selectField.options[0].selected) {
		return 0;
	}
	
	for (i = 1; i < length; i ++) {
		if (selectField.options[i].selected) {
			oOption.text = selectField.options[i - 1].text;
			oOption.value = selectField.options[i - 1].value;

			selectField.options[i - 1].text = selectField.options[i].text;
			selectField.options[i - 1].value = selectField.options[i].value;
			selectField.options[i - 1].selected = true;

			selectField.options[i].text = oOption.text;
			selectField.options[i].value = oOption.value;
			selectField.options[i].selected = false;
		}
	}
}

function moveOptionDown(selectField) {
	var i;
	var oOption = new Option();
	var length = selectField.options.length;

	if (length == 0 || selectField.options[length - 1].selected) {
		return 0;
	}
	
	for (i = length - 1; i >= 0; i --) {
		if (selectField.options[i].selected) {
			oOption.text = selectField.options[i + 1].text;
			oOption.value = selectField.options[i + 1].value;

			selectField.options[i + 1].text = selectField.options[i].text;
			selectField.options[i + 1].value = selectField.options[i].value;
			selectField.options[i + 1].selected = true;

			selectField.options[i].text = oOption.text;
			selectField.options[i].value = oOption.value;
			selectField.options[i].selected = false;
		}
	}
}

function removeTextNodes(node) {
	for (var i = 0; i < node.childNodes.length; i ++) {
		if (node.childNodes.item(i).nodeType == 3) {
			node.removeChild(node.childNodes.item(i));
		}
	}
}

function swapChildNodes(node, childNode1, childNode2) {
	var temp;
	
	if (childNode1 == childNode2) return;

	temp = childNode1.cloneNode(true);
	node.replaceChild(temp, childNode1);
	node.replaceChild(childNode1, childNode2);
	node.replaceChild(childNode2, temp);
}

function appendChildNodes(node, sourceNode) {
	var newSourceNode = sourceNode.cloneNode(true);
	var childNodes = newSourceNode.childNodes;
	var length = childNodes.length;

	while (childNodes.length > 0) {
		node.appendChild(childNodes.item(0));
	}
}

function removeChildNodes(node) {
	var childNodes = node.childNodes;
	var length = childNodes.length;

	while (childNodes.length > 0) {
		node.removeChild(childNodes.item(0));
	}
}

function replaceChildNodes(node, sourceNode) {
	var newSourceNode = sourceNode.cloneNode(true);
	
	removeChildNodes(node);
	appendChildNodes(node, newSourceNode);

	removeTextNodes(node);
	quicksortChildNodes(node, 0, node.childNodes.length - 1, "value");
}

function quicksortChildNodes(node, left, right, attributeName)	{
	var i, j;
	var pivot, temp;
	var childNodes = node.childNodes;
		
	if (left < right) {
		swapChildNodes(node, childNodes.item(parseInt((left + right) / 2)), childNodes.item(right));
		pivot = childNodes.item(right).getAttribute(attributeName);

		i = left - 1;
		j = right;

		for (;;) {
			while (childNodes.item(++ i).getAttribute(attributeName) < pivot);
			while (j > 0 && childNodes.item(-- j).getAttribute(attributeName) > pivot);

			if (i < j) {
				swapChildNodes(node, childNodes.item(i), childNodes.item(j));
			} else {
				break;
			}
		}
		swapChildNodes(node, childNodes.item(i), childNodes.item(right));

		quicksortChildNodes(node, left, i - 1, attributeName);
		quicksortChildNodes(node, i + 1, right, attributeName);
	}
}

function linearSearchChildNodes(node, attributeName, attributeValue, caseSensitive) {
	var childNodes = node.childNodes;

	if (!caseSensitive) {
		attributeValue = attributeValue.toLowerCase();
		
		for (var i = 0; i < childNodes.length; i ++) {
			if (childNodes.item(i).nodeType == 1) {
				if (childNodes.item(i).getAttribute(attributeName).toLowerCase() == attributeValue) {
					return childNodes.item(i);
				}
			}
		}
	} else {
		for (var i = 0; i < childNodes.length; i ++) {
 			if (childNodes.item(i).nodeType == 1) {
				if (childNodes.item(i).getAttribute(attributeName) == attributeValue) {
					return childNodes.item(i);
				}
			}
		}
	}
	return null;
}

function RegExpCommon() {
	this.EMAIL_ADDRESS = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]$";
	this.IP_ADDRESS = "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$";
	this.IP_MASK = "^(((255|254|252|248|240|224|192|128|0)\\.0\\.0\\.0)|(255\\.(254|252|248|240|224|192|128)\\.0\\.0)|(255\\.255\\.(254|252|248|240|224|192|128)\\.0)|(255\\.255\\.255\\.(255|254|252|248|240|224|192|128|0)))$";
	this.IP_RANGE = "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(-(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])|-((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])|\\/(3[0-2]|[1-2][0-9]|[0-9]))?$";
	this.MAC_ADDRESS = "^[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}$";
}

function checkFieldExp(field, regexpString, errorMessage) {
	var re = new RegExp(regexpString);

	if (!re.test(field.value.toString())) {
		alert(errorMessage);
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function getRadioValue(radioObj) {
	var i;

	for (i = 0; i < radioObj.length; i ++) {
		if (radioObj[i].checked) {
			if (radioObj[i].value) {
				return radioObj[i].value;
			} else {
				return i;
			}
		}
	}
	return null;
}
